package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.EcologyPointsDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.EcologyHatchRewardsConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.req.AddSmeltingRewardsConfRequest;
import com.sc.nft.enums.EcologyRewardsType;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchRewardsConfService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.service.impl
 * @Project：sc_metago
 * @name：EcologyHatchRewardsConfServiceImpl
 * @Date：2024/6/12 17:42
 * @Filename：EcologyHatchRewardsConfServiceImpl
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchRewardsConfServiceImpl implements EcologyHatchRewardsConfService {

    private final EquityPropsDao equityPropsDao;
    private final EcologyHatchRewardsConfDao ecologyHatchRewardsConfDao;
    private final EcologyPointsDao ecologyPointsDao;

    /**
     * 编辑熔炼方式奖励关联表
     *
     * @param ecologyHatchId         熔炼配置id
     * @param methodId               熔炼方式id
     * @param rewardsConfRequestList 奖励配置列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchRewardsConf(Long ecologyHatchId, Long methodId, List<AddSmeltingRewardsConfRequest> rewardsConfRequestList) {

        List<EcologyHatchRewardsConf> ecologyHatchRewardsConfDaoList = getList(methodId, ecologyHatchId);
        List<Long> idsInDb = ecologyHatchRewardsConfDaoList.stream().map(EcologyHatchRewardsConf::getId).collect(Collectors.toList());

        for (AddSmeltingRewardsConfRequest req : rewardsConfRequestList) {
            Long id = req.getId();
            if (req.getRewardsType() == EcologyRewardsType.ECOLOGY_POINTS) {
                req.setLinkName(EcologyRewardsType.ECOLOGY_POINTS.getDescription());
                req.setLinkImage(EcologyRewardsType.ECOLOGY_POINTS.getImage());
                req.setLinkCount(req.getLinkCount().setScale(2, BigDecimal.ROUND_DOWN));
                req.setLinkId(0L);
            } else if (req.getRewardsType() == EcologyRewardsType.ECOLOGY_MARK){
                EcologyPoints ecologyPoints = ecologyPointsDao.getById(req.getLinkId());
                Assert.notNull(ecologyPoints, () -> new GlobalRunTimeException("品牌积分不存在"));
                req.setLinkName(ecologyPoints.getPointsName());
                req.setLinkImage(ecologyPoints.getPointsImage());
            }else {
                //校验权益道具
                EquityProps equityProps = equityPropsDao.getById(req.getLinkId());
                Assert.notNull(equityProps, () -> new GlobalRunTimeException("权益道具不存在"));
                req.setLinkName(equityProps.getFirstTitle());
            }

            EcologyHatchRewardsConf ecologyHatchRewardsConf = new EcologyHatchRewardsConf();
            BeanUtils.copyProperties(req, ecologyHatchRewardsConf);
            if (ObjectUtil.isNull(id)) {
                //新增
                ecologyHatchRewardsConf.setMethodId(methodId);
                ecologyHatchRewardsConf.setEcologyHatchId(ecologyHatchId);
                ecologyHatchRewardsConf.insert();
            } else {//修改
                idsInDb.remove(id);
                EcologyHatchRewardsConf byIdAndEcologyHatchId = ecologyHatchRewardsConfDao.getByIdAndEcologyHatchId(id, ecologyHatchId, methodId);
                Assert.notNull(byIdAndEcologyHatchId, () -> new GlobalRunTimeException("生态熔炼奖励配置不存在"));
                Assert.isTrue(ecologyHatchRewardsConfDao.updateSmeltingRewardsConf(ecologyHatchRewardsConf), () -> new GlobalRunTimeException("生态熔炼奖励配置更新失败"));
            }
        }
        if (!CollectionUtils.isEmpty(idsInDb)){
            ecologyHatchRewardsConfDao.removeByIds(idsInDb);
        }
    }

    /**
     * 编辑熔炼方式奖励关联表
     *
     * @param methodId
     * @param ecologyHatchId
     * @return
     */
    @Override
    public List<EcologyHatchRewardsConf> getList(Long methodId, Long ecologyHatchId) {
        return ecologyHatchRewardsConfDao.getListByEcologyHatchId(ecologyHatchId, methodId);
    }
}
